﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using DataStore;
using DataStore.Domain;
using DataStore.Repositories;
using Infrastructure.CurrentData;
using Microsoft.Practices.Composite.Events;

namespace Infrastructure.ViewModels
{
    /// <summary>
    /// Company viewmodel
    /// </summary>
    public class CompanyViewModel : DataModelSingle<Company>, IDataErrorInfo
    {

        /// <summary>
        /// Initializes a new instance of the <see cref="CompanyViewModel"/> class.
        /// </summary>
        /// <param name="company">The company.</param>
        public CompanyViewModel(Company company)
            : base(company)
        {
            Initialize();
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="CompanyViewModel"/> class.
        /// </summary>
        /// <param name="eventAggregator">The event aggregator.</param>
        /// <param name="company">The company.</param>
        public CompanyViewModel(IEventAggregator eventAggregator, Company company) : base(eventAggregator, company)
        {
            Initialize();
        }

        /// <summary>
        /// Initializes this instance.
        /// </summary>
        void Initialize()
        {
            State = ModelState.Active;
        }

        /// <summary>
        /// Gets the company.
        /// </summary>
        /// <value>The company.</value>
        public Company Company
        {
            get
            {
                return Item;
            }
        }

        /// <summary>
        /// Gets or sets the id.
        /// </summary>
        /// <value>The id.</value>
        public virtual Guid Id { 
            get
            {
                return Item.Id;
            }
            set
            {
                if(Item.Id != value)
                {
                    Item.Id = value;
                    OnPropertyChanged("Id");
                }
            }
        }
        /// <summary>
        /// Gets or sets the name.
        /// </summary>
        /// <value>The name.</value>
        public virtual string Name
        {
            get
            {
                return Item.Name;
            }
            set
            {
                if(Item.Name != value)
                {
                    Item.Name = value;
                    OnPropertyChanged("Name");
                }
            }
        }
        /// <summary>
        /// Gets or sets the type.
        /// </summary>
        /// <value>The type.</value>
        public virtual CompanyType Type
        {
            get
            {
                return Item.Type;
            }
            set
            {
                if(Item.Type != value)
                {
                    Item.Type = value;
                    OnPropertyChanged("Type");
                }
            }
        }
        /// <summary>
        /// Gets or sets the date of foundation.
        /// </summary>
        /// <value>The date of foundation.</value>
        public virtual DateTime? Founded
        {
            get
            {
                return Item.Founded;
            }
            set
            {
                if(Company.Founded != value)
                {
                    Item.Founded = value;
                    OnPropertyChanged("Founded");
                }
            }
        }
        /// <summary>
        /// Gets or sets the founders.
        /// </summary>
        /// <value>The founders.</value>
        public virtual ICollection<Person> Employees
        {
            get
            {
                return Item.Employees;
            }
            set
            {
                if(Item.Employees != value)
                {
                    Item.Employees = value;
                    OnPropertyChanged("Employees");
                }
            }
        }
        /// <summary>
        /// Gets or sets the industry.
        /// </summary>
        /// <value>The industry.</value>
        public virtual string Industry
        {
            get
            {
                return Item.Industry;
            }
            set
            {
                if(Item.Industry != value)
                {
                    Item.Industry = value;
                    OnPropertyChanged("Industry");
                }
            }
        }
        /// <summary>
        /// Gets or sets the links.
        /// </summary>
        /// <value>The links.</value>
        public virtual ICollection<Link> Links
        {
            get
            {
                return Item.Links;
            }
            set
            {
                if(Item.Links != value)
                {
                    Item.Links = value;
                    OnPropertyChanged("Links");
                }
            }
        }
        /// <summary>
        /// Gets or sets the address.
        /// </summary>
        /// <value>The address.</value>
        public virtual ICollection<Address> Addresses
        {
            get
            {
                return Item.Addresses;
            }
            set
            {
                if(Item.Addresses != value)
                {
                    Item.Addresses = value;
                    OnPropertyChanged("Addresses");
                }
            }
        }


        /// <summary>
        /// Gets a value indicating whether the entity can be saved.
        /// </summary>
        /// <value><c>true</c> if the entity can be saved; otherwise, <c>false</c>.</value>
        protected override bool CanSave
        {
            get { return true; }
        }

        /// <summary>
        /// Saves the entity.
        /// </summary>
        public override void Save()
        {
            IDatabase database = Current.Container.Resolve<IDatabase>();
            using(var unitOfWork = database.CreateUnitOfWork())
            {

                try
                {
                    IRepository<Company> repository = new CompanyRepository(unitOfWork);
                    unitOfWork.BeginTransaction();
                    repository.Save(Item);
                    unitOfWork.CommitTransaction();
                }
                catch
                {
                    unitOfWork.RollbackTransaction();
                    throw;
                }
            }

            base.Save();
        }

        #region IDataErrorInfo Members

        public string Error
        {
            get { throw new NotImplementedException(); }
        }

        public string this[string columnName]
        {
            get { throw new NotImplementedException(); }
        }

        #endregion
    }
}